package com.scs.application.modules.orth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.modules.orth.dto.CheckDto;
import com.scs.application.modules.orth.entity.Check;
import com.scs.application.modules.orth.mapper.CheckMapper;
import com.scs.application.modules.orth.service.CheckService;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.sys.model.RestResult;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 骨科验收平台出库清点表 服务实现类
 * </p>
 */
@Service
public class CheckServiceImpl extends BaseServiceImpl<CheckMapper, Check> implements CheckService {

    @Override
    public RestResult createCheckByOrthScan(List<CheckDto> checkDtoList) {
        if (CollectionUtils.isEmpty(checkDtoList)) {
            throw new BusinessException("参数列表为空,请检查！");
        }
        List<String> keyList = checkDtoList.stream().map(CheckDto::getOipWiSn).distinct().collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(keyList)) {
            super.remove(new QueryWrapper<Check>().in("ref_key", keyList));
        }

        List<Check> checkList = new ArrayList<>();
        checkDtoList.stream().forEach(checkDto -> {
            Check check = new Check();
            check.setMatrId(checkDto.getGoodsCode())
                    .setPackId(checkDto.getPackageCode())
                    .setRefKey(checkDto.getOipWiSn())
                    .setUdi(checkDto.getUdi())
                    .setSkuQty(checkDto.getQty());
            checkList.add(check);
        });
        if (CollectionUtils.isEmpty(checkList)) {
            throw new BusinessException("参数列表为空,请检查！");
        }
        super.saveBatch(checkList);
        return RestResult.ok("保存成功");
    }
}
